/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest.front;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.bo.resp.QztyUploadResp;
import me.zhengjie.modules.system.domain.DccOss;
import me.zhengjie.modules.system.service.DccOssService;
import me.zhengjie.modules.system.service.DccQunzhongtianyanService;
import me.zhengjie.modules.system.util.OSSClientUtil;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.RedisUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

/**
* @website https://eladmin.vip
* @author cjl
* @date 2023-05-16
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "DCC文件上传相关管理")
@RequestMapping("/front/upload")
public class FrontUploadController {



    private final DccQunzhongtianyanService dccQunzhongtianyanService;

    private final RedisUtils redisUtils;
    private final DccOssService dccOssService;



    @Log("文件上传")
    @ApiOperation("文件上传")
    @AnonymousAccess
    @PostMapping(value = "/upload")
    public ResponseEntity<QztyUploadResp> upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception{

        boolean login = StpUtil.isLogin();
        QztyUploadResp qztyUploadResp=new QztyUploadResp();

        if (login){
            DccOss dccOss=new DccOss();
            String fileName = file.getOriginalFilename();
            File dest=new File("/tmp/"+fileName);
            try {
                file.transferTo(dest);
            } catch (Exception e) {
                log.error(e.toString(), e);
            }
            String currentDateTime = DateUtil.localDateTimeFormat(LocalDateTime.now(),DateUtil.DFY_MD_YY);
            String filePath="userInfo/"+StpUtil.getLoginId()+ "/"+ currentDateTime+dest.getName();
            try {
                OSSClientUtil.writeFileByFile(dest,filePath);
                //入库记录链接
                dccOss.setOperator(StpUtil.getLoginId().toString());
                dccOss.setUrl("https://rwoss.miaolianlian.com/"+filePath);
                dccOss.setType("正常文件上传");
            } catch (IOException e) {
                e.printStackTrace();
            }
            dccOssService.create(dccOss);
            qztyUploadResp.setImgUrl("https://rwoss.miaolianlian.com/"+filePath);

            return new ResponseEntity<QztyUploadResp>(qztyUploadResp, HttpStatus.OK);
        }else{
            return new ResponseEntity<QztyUploadResp>(qztyUploadResp, HttpStatus.BAD_GATEWAY);

        }
    }


    @Log("文件上传")
    @ApiOperation("文件上传")
    @AnonymousAccess
    @PostMapping(value = "/uploadV1")
    public ResponseEntity<QztyUploadResp> uploadV1(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception{

        QztyUploadResp qztyUploadResp=new QztyUploadResp();

            DccOss dccOss=new DccOss();
            String fileName = file.getOriginalFilename();
            File dest=new File("/tmp/"+fileName);
            try {
                file.transferTo(dest);
            } catch (Exception e) {
                log.error(e.toString(), e);
            }
            String currentDateTime = DateUtil.localDateTimeFormat(LocalDateTime.now(),DateUtil.DFY_MD_YY);
            String filePath="userInfo/"+ currentDateTime+dest.getName();
            try {
                OSSClientUtil.writeFileByFile(dest,filePath);
                //入库记录链接
                //dccOss.setOperator(StpUtil.getLoginId().toString());
                dccOss.setUrl("https://rwoss.miaolianlian.com/"+filePath);
                dccOss.setType("正常文件上传");
            } catch (IOException e) {
                e.printStackTrace();
            }
            dccOssService.create(dccOss);
            qztyUploadResp.setImgUrl("https://rwoss.miaolianlian.com/"+filePath);

            return new ResponseEntity<QztyUploadResp>(qztyUploadResp, HttpStatus.OK);
    }
}